import { createStore } from 'vuex'

export default createStore({
  state: {
    token: localStorage.getItem('token') || null,
    isAuthenticated: !!localStorage.getItem('token'),
    currentUser: null
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      state.isAuthenticated = !!token
      if (token) {
        localStorage.setItem('token', token)
      } else {
        localStorage.removeItem('token')
      }
    },
    setCurrentUser(state, user) {
      state.currentUser = user
    }
  },
  actions: {
    login({ commit }, token) {
      commit('setToken', token)
    },
    logout({ commit }) {
      commit('setToken', null)
      commit('setCurrentUser', null)
    },
    setUser({ commit }, user) {
      commit('setCurrentUser', user)
    }
  },
  modules: {
  }
})